package com.flying.flow.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.flow.model.Flow;
import com.flying.flow.model.Flow.FlowStatus;
import com.flying.flow.model.Flow.Scope;
import com.flying.flow.model.FlowDefinition;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;

@Service(value="FlowService", desc="业务流程")
public class FlowService extends AbstractService{
	private FlowDefinitionService fds;
	
	public void setFlowDefinitionService(FlowDefinitionService fds) {
		this.fds = fds;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=Flow.ENTITY_NAME)
	public Flow create(
			@Param(value=Flow.NAME, required=true,	desc="名称") String name,
			@Param(value=Flow.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Flow.ORG_CODE, required=false,	desc="机构代码") String org_code,
			@Param(value=Flow.BUSINESS_TYPE, required=true,	desc="业务类型") String business_type,
			@Param(value=Flow.SCOPE, required=false, enumClass=Scope.class,	desc="范围") Integer scope,
			@Param(value=Flow.STATUS, required=false, enumClass=FlowStatus.class,	desc="状态") Integer status,
			@Param(value=Flow.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=Flow.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(Flow.class, 
				Flow.CREATOR, (p == null?null: p.getName()),
				Flow.CREATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Flow.ENTITY_NAME)
	public ActionResult update(
			@Param(value=Flow.FLOW_ID, required=true,	desc="流程ID") Long flow_id,
			@Param(value=Flow.NAME, required=true,	desc="名称") String name,
			@Param(value=Flow.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Flow.ORG_CODE, required=false,	desc="机构代码") String org_code,
			@Param(value=Flow.BUSINESS_TYPE, required=true,	desc="业务类型") String business_type,
			@Param(value=Flow.CURRENT_FLOW_DEF, required=true,	desc="当前流程定义ID") Long current_flow_def,
			@Param(value=Flow.VERSION, required=false,	desc="当前版本") Integer version,
			@Param(value=Flow.SCOPE, required=false, enumClass=Scope.class,	desc="范围") Integer scope,
			@Param(value=Flow.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=Flow.UPDATE_TIME, required=false,	desc="修改时间") Date update_time
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				Flow.UPDATOR, (p == null?null: p.getName()),
				Flow.UPDATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Flow.ENTITY_NAME)
	public ActionResult updateStatus(
			@Param(value=Flow.FLOW_ID, required=true,	desc="流程ID") Long flow_id,
			@Param(value=Flow.STATUS, required=true, enumClass=FlowStatus.class,	desc="状态") Integer status,
			@Param(value=Flow.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=Flow.UPDATE_TIME, required=false,	desc="修改时间") Date update_time
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				Flow.UPDATOR, (p == null?null: p.getName()),
				Flow.UPDATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Flow.ENTITY_NAME)
	public ActionResult updateFlowDefinition(
			@Param(value=Flow.FLOW_ID, required=true,	desc="流程ID") Long flow_id,
			@Param(value=Flow.CURRENT_FLOW_DEF, required=true,	desc="当前流程定义ID") Long current_flow_def,
			@Param(value=Flow.VERSION, required=false,	desc="当前版本") Integer version,
			@Param(value=Flow.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=Flow.UPDATE_TIME, required=false,	desc="修改时间") Date update_time
		) throws Exception {
		FlowDefinition fd = this.fds.findById(current_flow_def);
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				Flow.VERSION, fd.getVersion(),
				Flow.UPDATOR, (p == null?null: p.getName()),
				Flow.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=Flow.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=Flow.FLOW_ID, required=true,	desc="流程ID") Long flow_id
		) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Flow.ENTITY_NAME, pageable=true, modelClass=Flow.class)
	public QueryResult<Flow> findAll(
			@Param(value=Flow.NAME, required=false,	desc="名称") String name,
			@Param(value=Flow.ORG_CODE, required=false,	desc="机构代码") String org_code,
			@Param(value=Flow.BUSINESS_TYPE, required=false,	desc="业务类型") String business_type,
			@Param(value=Flow.SCOPE, required=false, enumClass=Scope.class,	desc="范围") Integer scope,
			@Param(value=Flow.STATUS, required=false, enumClass=FlowStatus.class,	desc="状态") Integer status,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Flow.ENTITY_NAME, single=true, throwsNotFoundException=true, wsql="scope=0" )
	public Flow findByBusinessType(
			@Param(value=Flow.BUSINESS_TYPE, required=true,	desc="业务代码") String business_type
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Flow.ENTITY_NAME, single=true, throwsNotFoundException=true, wsql="scope=1" )
	public Flow findByOrgBusinessType(
			@Param(value=Flow.ORG_CODE, required=true,	desc="机构代码") String org_code,
			@Param(value=Flow.BUSINESS_TYPE, required=true,	desc="业务代码") String business_type
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Flow.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Flow findById(
			@Param(value=Flow.FLOW_ID, required=true,	desc="流程ID") Long flow_id
		) throws Exception {
		return null;
	}

}